package nl.hr.cmi.project4.inf1m.groep1.ZuinigRijden;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import android.app.Activity;
import android.text.format.Time;

public class LocationListGPX {
	Activity activity;

	public LocationListGPX(Activity a) {
		activity = a;
	}

	public void saveRoute(ArrayList<SmallLocation> locationList,
			String routeName) {

		try {
			BufferedWriter file = new BufferedWriter(new OutputStreamWriter(
					activity.openFileOutput(routeName + ".gpx",
							Context.MODE_WORLD_WRITEABLE)));
			for (SmallLocation loc : locationList) {
				Log.d("saveRoute", "Adding GPX waypoint");
				file.write(new Double(loc.getLatitude()).toString());
				file.newLine();
				file.write(new Double(loc.getLongitude()).toString());
				file.newLine();
				file.write(new Float(loc.getSpeed()).toString());
				file.newLine();
				file.write(new Long(loc.getTime()).toString());
				file.newLine();
			}
			file.close();
			Log.d("saveRoute", "Ended xml creation");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public File[] getRoutes() {
		File rootDir = activity.getFilesDir();
		File[] fileList = rootDir.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				String suffix = ".gpx";
				if (pathname.getName().toLowerCase().endsWith(suffix)) {
					return true;
				}
				return false;
			}
		});
		return fileList;
	}

	public ArrayList<SmallLocation> getRoute(String fileName) {
		ArrayList<SmallLocation> locationList = new ArrayList<SmallLocation>();
		
		BufferedReader bufferedReader;
		try {
			
			bufferedReader = new BufferedReader(new FileReader(activity.getFileStreamPath(fileName)));
			
			//bufferedReader = new BufferedReader(new FileReader(
					//fileName));
			String line;
			SmallLocation smallLocation = new SmallLocation();
			int count = 0;
			while ((line = bufferedReader.readLine()) != null) {
				if(line == ""){
					break;
				}
				Log.d("OpenRoute", line);
				
				if(count == 0){
					smallLocation.setLatitude(Double.parseDouble(line));
					count++;
				} else if (count == 1){
					smallLocation.setLongitude(new Double(line));
					count++;
				} else if (count == 2){
					smallLocation.setSpeed(new Float(line));
					count++;
				} else {
					smallLocation.setTime(new Long(line));
					locationList.add(smallLocation);
					smallLocation = new SmallLocation();
					count = 0;
				}
			}
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			Toast.makeText(activity,
	                "File not found.",
	                Toast.LENGTH_LONG).show();
		} catch (IOException e){
			Toast.makeText(activity,
	                "Something went wrong :(",
	                Toast.LENGTH_LONG).show();
		}
		

		return locationList;
	}

	public int routeCount() {
		File rootDir = activity.getFilesDir();
		File[] fileList = rootDir.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				String suffix = ".gpx";
				if (pathname.getName().toLowerCase().endsWith(suffix)) {
					return true;
				}
				return false;
			}
		});
		return fileList.length;
	}

	public String timeStamp(long t) {
		Time time = new Time();
		time.set(t);
		// 2001-11-16T23:03:38Z
		String _return = time.year + "-" + time.month + "-" + time.monthDay
				+ "T" + time.hour + ":" + time.minute + ":" + time.second + "Z";
		return _return;
	}//
}
